package priv.blackbibo.application.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import priv.blackbibo.application.R;

public class NoticeActivity extends BaseActivity  {

    private static final String TAG = "NoticeActivity";

    private View view1 = null;
    private TextView timeView = null;
    private View layout = null;
    private ImageView cup = null;
    private float lastx = 0;
    private float lasty = 0;

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);

        cup = findViewById(R.id.cup);

        view1 = findViewById(R.id.scrollBtn1);
        Animation alpha1 = new AlphaAnimation(0,1);
        alpha1.setDuration(1000);
        alpha1.setStartOffset(0);
        alpha1.setRepeatCount(200);
        alpha1.setRepeatMode(Animation.REVERSE);
        view1.setAnimation(alpha1);

        timeView = findViewById(R.id.timeView);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(getString(R.string.time_hour_and_seconds_patterns));
        timeView.setText(simpleDateFormat.format(new Date()));

        layout = findViewById(R.id.layout);
        layout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Log.d(TAG, "onTouch: down " + event.getX() + "," + event.getY());
                    lastx = event.getX();
                    lasty = event.getY();
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    Log.d(TAG, "onTouch: up"+ event.getX() + "," + event.getY());
                    float rotation = cup.getRotation();
                    if (rotation < -30){
                        MainActivity.callStart();
                        handler.post(()->{
                            MainActivity.callStart();
                            finish();
                        });
                    }
                } else {
                    float rotation = cup.getRotation();
                    float distance = event.getY() - lasty;
                    float result_rotation = rotation + distance / 20;
                    if (result_rotation < - 40 || result_rotation > 40){
                        return true;
                    }else {
                        cup.setRotation(result_rotation);
                    }
                    lastx = event.getX();
                    lasty = event.getY();
                    Log.d(TAG, "onTouch: "+ event.getAction() +" "+ event.getX() + "," + event.getY()  +"\t" + event.getRawX() + "," +event.getRawY());
                }
                return true;
            };
        });
    }
}
